package com.feicheng.common.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

public class DateUtils {

    private static HashMap<String, DateTimeFormatter> patternMap = new HashMap<>();

    static {
        patternMap.put("yyyyMMdd", DateTimeFormatter.ofPattern("yyyyMMdd"));
        patternMap.put("yyyy-MM-dd", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        patternMap.put("yyyy-MM-dd hh:mm:ss", DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
    }

    public static LocalDate coverToLocalDateByMills(long millisTimestamp) {
        long secondsTimestamp = millisTimestamp / 1000;
        Instant instant = Instant.ofEpochSecond(secondsTimestamp);
        return instant.atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime coverToLocalDateTimeByMills(long millisTimestamp) {
        long secondsTimestamp = millisTimestamp / 1000;
        Instant instant = Instant.ofEpochSecond(secondsTimestamp);
        return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static String dateToString(String pattern, LocalDate localDate) {
        return localDate.format(patternMap.get(pattern));
    }

    public static LocalDate stringToDate(String pattern, String datestr) {
        return LocalDate.parse(datestr, patternMap.get(pattern));
    }

    public static String getWeekStr(int week) {
        if (week == 1) {
            return "一";
        } else if (week == 2) {
            return "二";
        } else if (week == 3) {
            return "三";
        } else if (week == 4) {
            return "四";
        } else if (week == 5) {
            return "五";
        } else if (week == 6) {
            return "六";
        } else if (week == 7) {
            return "日";
        }
        return null;
    }
}
